<!DOCTYPE html>
<html>
<head>
    <title>住院结算--中软高科-2015</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="../Css/style.css"/>
    <script type="text/javascript" src="../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }

        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
    <script type="text/javascript">
        $(function () {
            $('#newNav').click(function () {
                window.location.href = "dispensing-gives.html";
            });
        });

        function checkall() {
            var alls = document.getElementsByName("check");
            var ch = document.getElementById("checkall");
            if (ch.checked) {
                for (var i = 0; i < alls.length; i++) {
                    alls[i].checked = true;
                }
            } else {
                for (var i = 0; i < alls.length; i++) {
                    alls[i].checked = false;
                }
            }
        }

        function delAll() {
            var alls = document.getElementsByName("check");
            var ids = new Array();
            for (var i = 0; i < alls.length; i++) {
                if (alls[i].checked) {
                    ids.push(alls[i].value);
                }
            }
            if (ids.length > 0) {
                if (confirm("确认操作?")) {
                    alert("成功!");
                }
            } else {
                alert("请选中要操作的项");
            }
        }
    </script>
</head>
<body>

<form id="accountForm" class="definewidth m20">
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td width="10%" class="tableleft">病例号：</td>
            <td><input type="text" name="id" id="id" value=""/></td>
            <td width="10%" class="tableleft">姓名：</td>
            <td><input type="text" name="registerName" id="registerName" value=""/></td>
        </tr>
        <tr>
            <td colspan="4">
                <center>
                    <input class="btn btn-primary" type="button" onclick="loadData()" value="查询">
                    <input class="btn btn-primary" type="button" value="清空">
                </center>
            </td>
        </tr>
    </table>
</form>

<table class="table table-bordered table-hover definewidth m10">
    <thead>
    <tr>
        <th><input type="checkbox" id="checkall" onChange="checkall();"></th>
        <th>病历号</th>
        <th>姓名</th>
        <th>押金</th>
        <th>当前余额</th>
        <th>状态</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody id="tbody">

    </tbody>
</table>

<table class="table table-bordered table-hover definewidth m10">
    <tr>
        <th colspan="5">
            <div class="inline pull-right page">

                <a name="" id="firstPage" onclick="A()">第一页</a>
                <a name="" id="prePage" onclick="B()">上一页</a>
                <span id="currentPage"></span>
                <a name="" id="nextPage" onclick="C()">下一页</a>
                <a name="" id="lastPage" onclick="D()">最后一页</a>
                &nbsp;&nbsp;&nbsp;共<span id="total" class='current'></span>条记录
                <span id="pageNum"></span>/<span id="pages"></span>页
                <span id="pageSize" class="hidden"></span>

            </div>
            <div>
                <button type="button" class="btn btn-success" onclick="exportInfo()">导出Excel</button>
                导出文件位置:
                <input type="text" id="outStream" name="outStream">
            </div>

        </th>
    </tr>
</table>
<script>
    loadData()

    function A() {
        if ($("#firstPage").attr("class") == "btn btn-small disabled") {
            return;
        }
        let num = $("#firstPage").attr("name");
        $("#pageNum").html(num);
        loadData();
    };

    function B() {
        if ($("#prePage").attr("class") == "btn btn-small disabled") {
            return;
        }
        let num = $("#prePage").attr("name");
        $("#pageNum").html(num);
        loadData();
    };

    function C() {
        if ($("#nextPage").attr("class") == "btn btn-small disabled") {
            return;
        }
        let num = $("#nextPage").attr("name");
        $("#pageNum").html(num);
        loadData();
    };

    function D() {
        if ($("#lastPage").attr("class") == "btn btn-small disabled") {
            return;
        }
        let num = $("#lastPage").attr("name");
        $("#pageNum").html(num);
        loadData();
    };

    function E(j) {
        $("#pageNum").html(j);
        loadData();
    };

    function fillPageData(pageInfo) {
        //填充信息
        $("#total").html(pageInfo.total);
        $("#pages").html(pageInfo.pages);
        $("#pageNum").html(pageInfo.pageNum);
        $("#pageSize").html(pageInfo.pageSize);
        let str = "";
        for (let i = 1; i <= pageInfo.pages; i++) {
            str += '<a onclick=E(' + i + ')>' + i + '</a>';
        }
        $("#currentPage").html(str);

        //设置按钮的属性值和样式
        $("#firstPage").attr("name", 1);
        $("#prePage").attr("name", pageInfo.prePage);
        $("#nextPage").attr("name", pageInfo.nextPage);
        $("#lastPage").attr("name", pageInfo.pages);
        $("#firstPage").removeClass("btn btn-small disabled");
        $("#prePage").removeClass("btn btn-small disabled");
        $("#nextPage").removeClass("btn btn-small disabled");
        $("#lastPage").removeClass("btn btn-small disabled");
        if (pageInfo.isFirstPage) {//如果是第一页：上一页和首页禁用
            $("#firstPage").addClass("btn btn-small disabled");
            $("#prePage").addClass("btn btn-small disabled");
            $("#current1").addClass("btn btn-small disabled");
        }
        if (pageInfo.isLastPage) {//如果已经是最后一页：下一页和末页禁用
            $("#nextPage").addClass("btn btn-small disabled");
            $("#lastPage").addClass("btn btn-small disabled");
        }
    }

    function loadData() {
        let pageNum = $("#pageNum").html();
        let pageSize = $("#pageSize").html();
        let id = $("#id").val();
        let registerName = $("#registerName").val();
        $.ajax({
            url: "/InpatientSettlementController/PaginationSearch",
            type: 'GET',
            data: "id=" + id + "&registerName=" + registerName + "&pageNum=" + pageNum + "&pageSize=" + pageSize,
            dataType: 'json',
            success: function (vo) {
                console.log(vo);
                let str = "";
                let Settlement="";
                let data = vo.pageInfo.list;
                for (let i = 0; i < data.length; i++) {
                    let hosR_id = data[i].id;//病历号
                    let hosR_name = data[i].registerName;//姓名
                    let beH_antecedent = (data[i].hospital.deposit == null ? '无押金' : data[i].hospital.deposit);//押金
                    let rest_Cost = data[i].hospital.restCost;//余额
                    let hosR_endstatus = data[i].hospital.endStatus == 1 ? '已结算' : '未结算';//住院状态
                    if (data[i].hospital.endStatus == 1) {
                         Settlement = '<td style="vertical-align:middle;">' +
                            '<a href="account-look.html?hosR_id=' + data[i].id + '&hosR_name=' + hosR_name +
                            '&beH_antecedent=' + beH_antecedent + '">详细信息</a>' +
                            '&nbsp;&nbsp;&nbsp;已结算</td>'
                    }
                    else {
                         Settlement = '<td style="vertical-align:middle;">' +
                            '<a href="account-look.html?hosR_id=' + data[i].id + '&hosR_name=' + hosR_name +
                            '&beH_antecedent=' + beH_antecedent + '">详细信息</a>' +
                            '&nbsp;&nbsp;&nbsp;<a href="" onclick="end('+data[i].id+','+beH_antecedent+')">结算</a></td>'
                    }
                    str += '<tr>' +
                        '        <td style="vertical-align:middle;"><input type="checkbox" name="check" value="1"></td>\n' +
                        '        <td style="vertical-align:middle;">' + hosR_id + '</td>\n' +
                        '        <td style="vertical-align:middle;">' + hosR_name + '</td>\n' +
                        '        <td style="vertical-align:middle;">' + beH_antecedent + '元</td>\n' +
                        '        <td style="vertical-align:middle;">' + rest_Cost + '元</td>\n' +
                        '        <td style="vertical-align:middle;">' + hosR_endstatus + '</td>\n' +
                        Settlement
                        +
                        '    </tr>'
                }
                $("#tbody").html(str);
                fillPageData(vo.pageInfo);
            }
        })
    }

    function exportInfo() {
        let id = $("#registerName").val();
        let registerName = $("#registerName").val();
        let outStream = $("#outStream").val();
        if (outStream == "") {
            alert("保存路径为空");
            return false;
        }
        $.ajax({
            type: "POST",
            data: "outStream=" + outStream + "&id=" + id + "&registerName=" + registerName,
            dataType: "json",
            url: "/InpatientSettlementController/excelInfo",
            success: function (vo) {
                alert(vo.msg)
                if (vo.code == 200) {
                    black();
                }
            }
        })
    }
    function end(registerId,beH_antecedent) {
        $.ajax({
            url: "/InpatientSettlementController/PaginationSearchForEnd",
            type: 'GET',
            async:false,
            data: "registerId=" + registerId ,
            dataType: 'json',
            success: function (vo) {
                let total = 0;
                let data = vo.pageInfo.list;
                for (let i = 0; i < data.length; i++) {
                    let price = data[i].outPrice;
                    let ChargeDate = data[i].chargeDate;
                    if (ChargeDate==null){total += price;}
                }
                let restCost = (beH_antecedent - total);
                if(restCost>=0){
                $.ajax({
                    url: "/InpatientSettlementController/Settlement",
                    type: 'POST',
                    async:false,
                    data: "registerId=" + registerId+"&total="+total+"&restCost="+restCost ,
                    dataType: 'json',
                    success:function (vs){
                        alert("结算成功")
                    }
                })
            }
            else {
                alert("余额不足，请充值")
                }
            }

        })
    }
</script>
</body>
</html>
